let Base64 = require('js-base64').Base64
// initial state
const state = {
  user: {},
  role: [],
  district: {},
  module: [],
  button: []
}

// getters
const getters = {
  getUser: state => {
    return state.user
  },
  getButton: state => {
    return state.button
  },
  getRole: state => {
    return state.role
  },
  getDistrict: state => {
    return state.district
  },
  dealedModule: state => {
    const temArr = []
    const copy = state.module
    copy.forEach(item => {
      if (item.children.length !== 0) {
        item.expand = false
      }
      temArr.push(item)
    })
    return temArr
  },
  getModule: state => {
    return state.module
  },
}

/**
 * 获取所有有权限的path
 * */
function getAllPath (paths, module) {
  const BUTTON = 2
  module.forEach(item => {
    if (item.type === BUTTON) {
      return
    }
    if (item.children.length === 0) { // 一级模块
      paths.push(item.url)
    } else {
      getAllPath(paths, item.children)
    }
  })
}

// mutations
const mutations = {
  /**
   * 改变module
   * */
  setModule (state, module) {
    state.module = null
    state.module = module
    const paths = []
    getAllPath(paths, module)
    sessionStorage.setItem('paths', Base64.encode(JSON.stringify(paths)))
  },
  /**
   * 设置button
   * */
  setButton (state, obj) {
    state.button = null
    state.button = obj
    sessionStorage.setItem('buttons', Base64.encode(JSON.stringify(obj)))
  },
  /**
   * 设置user
   * */
  setUser (state, obj) {
    state.user = null
    state.user = obj
  },
  /**
   * 设置role
   * */
  setRole (state, obj) {
    state.role = null
    state.role = obj
  },
  /**
   * 设置district
   * */
  setDistrict (state, obj) {
    state.district = null
    state.district = obj
  }
}

// actions
const actions = {

}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
